
#include "timeLearn.h"
#include <iostream>
#include <ctime>
#include <cmath>
#include <chrono>

using namespace std;
using namespace std::chrono;

long Fibonacci(int n){
    return n < 2 ? n : Fibonacci(n-1) + Fibonacci(n-2);
}
timeLearn::timeLearn()
{
    cout << "timeLearn::timeLearn()" << endl;
}
timeLearn::~timeLearn()
{
    cout << "timeLearn::~timeLearn()" << endl;
}
void timeLearn::timeStd()
{
    time_t now = time(0);
    cout << "time_t: " << now << endl;
    cout << "time: " << ctime(&now) << endl;
}

namespace times
{
    void testCTime()
    {   //time_t对应long long类型
        time_t tStart = time(nullptr);
        //对应的类型为long，cpu计时的一种类型
        clock_t start = clock();
        char * ct = ctime(&tStart);
        //tm结构体
        tm * clt = localtime(&tStart);
        //tm结构体
        tm * gmt = gmtime(&tStart);
        char * asct = asctime(clt);
        clock_t end = clock();
        time_t tEnd = time(nullptr);
        //double类型
        auto dif = difftime(tEnd, tStart);
        cout<<" ======time======"<<asct<<endl;
        cout << "diff time "<<dif << endl; 
        char buffer[80]; 
        //注意返回类型size_t（字符串的长度），本质上是无符号的long long类型
        size_t sz = strftime(buffer,80,"%Y-%m-%d %H:%M:%S",clt);
        cout<< "current time:" <<buffer<< endl;
        cout << "==========timeLearn::testCout() spend===========" << end - start<< endl;
    }

    /**
     * C++ 20引入的choron库
     */
    void testChrono(){
        //sys_time类型（std:chrono::sys_time）
        const time_point now{system_clock::now()};
        const time_point start{steady_clock::now()};
        time_point hrc = high_resolution_clock::now();
        Fibonacci(42);
        //持续时间类型（std::chrono::duration）
        const auto end{steady_clock::now()};
        const auto duration{end - start};
        const auto d = end - start;
        const auto duration_ms{duration_cast<milliseconds>(duration)};
        const auto duration_us{duration_cast<microseconds>(duration)};
        const auto duration_ns{duration_cast<nanoseconds>(duration)};
        cout << "duration: " << duration.count() << "ns" << endl;
        auto d1 = 24h + 30min + 5s;

        auto tomorrow = now + d1;
        auto yesterday = now - d1;

        cout << "tomorrow:" << system_clock::to_time_t(tomorrow) << endl;


    }
}
